package cn.edu.tju.fctl.util;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by catold on 15/12/17.
 */
public class VacationType {
    @NotNull
    private Integer id; // 唯一标注
    private String name; // 类型名称
    private Integer duration; // 请假期限,按天算

    private List<VacationType> subTypes = new ArrayList<VacationType>(); // 可延期类型

    public VacationType(Integer id, String name, Integer duration) {
        this.id = id;
        this.name = name;
        this.duration = duration;
    }

    /**
     * 统计总和假期天数
     * @return
     */
    public int getAllDuration(){
        int amount = duration;
        if(subTypes != null) {
            for (VacationType type : subTypes) {
                amount += type.getDuration();
            }
        }
        return amount;
    }

    /**
     * 判断是否有子类型
     * @return
     */
    public boolean hasSubTypes(){
        return subTypes != null && subTypes.size() > 0;
    }

    /**
     * 判断是否子类存在
     * @param vacationType
     * @return
     */
    public boolean containsSubType(VacationType vacationType){
        boolean result = false;
        for ( VacationType vacation : subTypes) {
            if (vacationType.equals(vacation)) {
                result = true;
            }
        }
        return result;
    }

    public boolean containsSubType(Integer id){
        boolean result = false;
        for ( VacationType vacation : subTypes) {
            if (vacation.getId().equals(id)) {
                result = true;
            }
        }
        return result;
    }

    public VacationType getSubType(Integer id){
        for ( VacationType vacation : subTypes) {
            if (vacation.getId().equals(id)) {
                return vacation;
            }
        }

        return null;
    }

    /**
     * 添加子类
     * @param vacationType
     */
    public void addSubVacationType(VacationType vacationType){
        if(vacationType == null){
            return;
        }
        if(subTypes == null){
            subTypes = new ArrayList<VacationType>();
        }
        subTypes.add(vacationType);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getDuration() {
        return duration;
    }

    public void setDuration(Integer duration) {
        this.duration = duration;
    }

    public List<VacationType> getSubTypes() {
        return subTypes;
    }

    public void setSubTypes(List<VacationType> subTypes) {
        this.subTypes = subTypes;
    }

    @Override
    public boolean equals(Object obj) {
        if(obj == null || !(obj instanceof VacationType)){
            return false;
        }
        VacationType temp = (VacationType) obj;

        return temp.getId().equals(this.getId()) && temp.getName().equals(this.getName());
    }
}
